package com.hsc.db.tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hsc.account.oneActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.animation.BounceInterpolator;

public class UseItemService {

	public  DBOpenHelper dbOpenHelper;
	public  SQLiteDatabase  db =null;
	public UseItemService(Context context)
	{
		dbOpenHelper = new DBOpenHelper(context);
//		db= dbOpenHelper.getWritableDatabase();
		db = oneActivity.db;
	}


	public void save(useitem item){


		db.execSQL("insert into useitem(usetype,flag) values(?,?)",
				new Object[]{item.getUsetype(),item.getFlag()});
	}


	public void deleteByType (String type){
//		 SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();

		db.execSQL("delete from useitem where usetype=?",
				new Object[]{type});
	}

	public long getItemByType(String  typeName)
	{
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
		Cursor cur = db.rawQuery("select count(*) from useitem where usetype=?",new String[]{typeName});
		cur.moveToFirst();
		long count = cur.getLong(0);
		cur.close();
		return count;
	}

	public void updateFlag(String typeName,String flag){
//        SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();

		db.execSQL("update useitem set flag=? where usetype=?",
				new Object[]{flag,typeName});
	}

	public List<useitem> getDataFailed()
	{
		List<useitem> list = new ArrayList<useitem>();
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
		Cursor cur = db.rawQuery("select * from useitem where flag!=?",
				new String[]{"3"});


		while(cur.moveToNext())
		{
			useitem item  =new useitem();

			String usetype = cur.getString(cur.getColumnIndex("usetype"));
			String flag = cur.getString(cur.getColumnIndex("flag"));

			item.setFlag(flag);
			item.setUsetype(usetype);
			list.add(item);
		}
		cur.close();
		return list;
	}

	public ArrayList<useitem> getDataSuc()
	{
		ArrayList<useitem> list = new ArrayList<useitem>();
		Cursor cur = db.rawQuery("select * from useitem where flag!=?",
				new String[]{"0"});


		while(cur.moveToNext())
		{
			useitem item  =new useitem();

			String usetype = cur.getString(cur.getColumnIndex("usetype"));

			item.setFlag("1");
			item.setUsetype(usetype);
			list.add(item);
		}
		cur.close();
		return list;
	}


	public double getSumByType(String type,int year,int month)
	{
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();

		Cursor cur=  db.rawQuery("select sum(amt) from acclist where balance<>0 and date>=? and date <=? and use=?",new String[]{String.valueOf(year)+"-"+String.format("%02d", month)+"-"+"01",String.valueOf(year)+"-"+String.format("%02d", month)+"-"+"31",type});


		cur.moveToFirst();
		double count= cur.getDouble(0);
		cur.close();

		return count;


	}

	public List<Map<String, Object>> getData(int year,int month)
	{
		int count=0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
		Cursor cur = db.rawQuery("select * from useitem where flag!=? order by id asc",	new String[]{"0"});
		while(cur.moveToNext())
		{
			count++;

			Map<String, Object> map = new HashMap<String, Object>();
			String type=cur.getString(cur.getColumnIndex("usetype"));
			map.put("type", type);
			double tsum = getSumByType(type,year,month);

			if(tsum==0)
			{
				map.put("tsum", " ");
			}else
			{
				map.put("tsum", String.format("%.1f",tsum));
			}

			list.add(map);

		}

		Collections.sort(list,new Comparator () {
			@Override
			public int compare(Object o1, Object o2) {

				HashMap<String, String> map1 = ( HashMap<String, String>)o1;
				HashMap<String, String> map2 = ( HashMap<String, String>)o2;

				if(!map1.get("tsum").equals(" ")&&!map2.get("tsum").equals(" "))
				{
					double ret= Double.parseDouble(map2.get("tsum").toString())-Double.parseDouble(map1.get("tsum").toString());
					return (int) ret;
				}else if(map1.get("tsum").equals(" ")&&!map2.get("tsum").equals(" "))
				{
					return 1;
				}else  if(!map1.get("tsum").equals(" ")&&map2.get("tsum").equals(" "))
				{
					return -1;
				}

				return 0;

			}
		});

		if(count!=0 && count%7!=0)
		{
			int size = 7- count%7;
			for(int i=0;i<size;i++)
			{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", " ");
				map.put("tsum", " ");
				list.add(map);
			}
		}

		cur.close();
		return list;
	}

	public List<Map<String, Object>> getGroupData(int year,int month)
	{
		int count=0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Cursor cur=  db.rawQuery("select use,sum(amt) from acclist where balance<>0 and date>=? and date <=? and use is not null group by use",new String[]{String.valueOf(year)+"-"+String.format("%02d", month)+"-"+"01",String.valueOf(year)+"-"+String.format("%02d", month)+"-"+"31"});
		while(cur.moveToNext())
		{
			Map<String, Object> map = new HashMap<String, Object>();
			String type=cur.getString(0);

			if(type.equals("null"))
			{
				continue;
			}

			count++;

			map.put("type", type);
			double tsum = cur.getDouble(1);

			if(tsum==0)
			{
				map.put("tsum", " ");
			}else
			{
				map.put("tsum", String.format("%.1f",tsum));
			}

			list.add(map);

		}

		Collections.sort(list,new Comparator () {
			@Override
			public int compare(Object o1, Object o2) {

				HashMap<String, String> map1 = ( HashMap<String, String>)o1;
				HashMap<String, String> map2 = ( HashMap<String, String>)o2;

				double ret= Double.parseDouble(map2.get("tsum").toString())-Double.parseDouble(map1.get("tsum").toString());
				return (int) ret;


			}
		});

		if(count!=0 && count%7!=0)
		{
			int size = 7- count%7;
			for(int i=0;i<size;i++)
			{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", " ");
				map.put("tsum", " ");
				list.add(map);
			}
		}




		cur.close();
		return list;
	}

	public List<Map<String, Object>> getYearGroupData(int year)
	{
		int count=0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Cursor cur=  db.rawQuery("select use,sum(amt) from acclist where balance<>0 and date>=? and date <=? and use is not null group by use",new String[]{String.valueOf(year)+"-01-01",String.valueOf(year)+"-12-31"});
		while(cur.moveToNext())
		{
			Map<String, Object> map = new HashMap<String, Object>();
			String type=cur.getString(0);

			if(type.equals("null"))
			{
				continue;
			}

			count++;

			map.put("type", type);
			double tsum = cur.getDouble(1);

			if(tsum==0)
			{
				map.put("tsum", " ");
			}else
			{
				map.put("tsum", String.format("%.1f",tsum));
			}

			list.add(map);

		}

		Collections.sort(list,new Comparator () {
			@Override
			public int compare(Object o1, Object o2) {

				HashMap<String, String> map1 = ( HashMap<String, String>)o1;
				HashMap<String, String> map2 = ( HashMap<String, String>)o2;

				double ret= Double.parseDouble(map2.get("tsum").toString())-Double.parseDouble(map1.get("tsum").toString());
				return (int) ret;


			}
		});

		if(count!=0 && count%6!=0)
		{
			int size = 6- count%6;
			for(int i=0;i<size;i++)
			{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", " ");
				map.put("tsum", " ");
				list.add(map);
			}
		}


		cur.close();
		return list;
	}

}
